var dept = function(){
	this.box = $("#formpage");	//表单
	this.input = this.box.find("span.at_text>input");
	this.deptName = $("#deptName");//设施名称
	this.pnameCheck = false;
	
	this.btn = $("#reg_sub");	//提交按钮

	var me = this;  //page  1新增 2修改 
	
	var isloading=false;
	
	//提示信息
	var h_on='<em class="on">';	
	var h_err = '<em class="orr"><u class="at_msg m_3_e"></u>';
	var deptName_o = h_on+'<span id="namecount">·请输入部门名称</span></em>';
	
	//错误
	var deptName_e1 = h_err+'部门名称不能为空</em>';
	
	//正确
	var name_g = '<em><u class="at_msg m_3_o"></u></em>';
	var name_ld = '<em class="ld"><u class="at_msg at_write"></u>正在检测中……</em>';

	function txt(t){var a=t.parents(".at_text");return a};	
	this.input.focus(function(){//获得焦点
		txt($(this)).attr("class","at_text t_1_i");		
		txt($(this)).find("em").remove();	
		$(this).parents("dd").css("z-index",999);
	})
	this.input.blur(function(){//失去焦点
		txt($(this)).attr("class","at_text t_1_d");
		txt($(this)).find("em").remove();
		$(this).parents("dd");
	})	
	var inErr = function(a,b){//错误
		var at_text = txt(a);
		at_text.attr("class","at_text t_1_e");
		at_text.find("em").remove();
		at_text.append(b);
		return false;
	}
	var inOk = function(a){//正确
		var at_text = txt(a);
		at_text.attr("class","at_text t_1_o");
		at_text.find("em").remove();
		at_text.append(name_g);
	}
	var inPass = function(a){//正常，不加“正确”图标
		var at_text = txt(a);
		at_text.attr("class","at_text t_1_o");
		at_text.find("em").remove();
		//at_text.append(name_g);
	}
	
	var namelength = function(a) {//统计字符长度，中文算二个字符
		var name = a.val(),
			length = 0;
		for(var i=0; i<name.length; i++) {
			if(/[\u4e00-\u9fa5]/.test(name.charAt(i))) length+=2;
			else length+=1;
		}
		return length;
	};
	
	var strTrimLength = function(a) {//统计字符长度，不包括空格在内
		var name = a.val(),
			length = 0;
		name=$.trim(name);
		for(var i=0; i<name.length; i++) {
			if(/[\u4e00-\u9fa5]/.test(name.charAt(i))) length+=2;
			else length+=1;
		}
		return length;
	};
	
	var clearEmpty = function(O) {//禁止输入空格
		O.val(O.val().replace(/\s*/g,""));
	}
	
	this.deptNameCheck = function(i,s){//验证设施名称	
		var len = strTrimLength(i);
        if (len == 0) {//8-50字符 (1汉字等于2字符)
            me.pnameCheck = inErr(i,deptName_e1);			
			return;
        }
		inPass(i);				
		me.pnameCheck = true;
		//if(s) me.regIn();
	}
	
	this.btn.click(function(){//按钮提交
	    if(isloading){return false;}
		if(me.pnameCheck==false)
			me.deptNameCheck(me.deptName,false);
		if(! me.pnameCheck) return false;
		
		return me.box.submit();
		//me.regIn();
		//return true;
	})
	
	this.regIn = function(){
		//if(me.sCheck==false) {at_infoTrace.show('请输入正确的封条号！','i');return;}
		//if(me.pnameCheck==false) {at_infoTrace.show('请输入设施名称！','i');return;}
		//if(me.pnoCheck==false) {at_infoTrace.show('请输入设施编号！','i');return;}
		alert("regIn");
		at_loading.show();
		isloading=true;	
		$.ajax({
		    url: "ajaxSafety_add.do", //me.box.attr("action"),
		    type:"POST",
		    data:me.box.serialize(),
		    success:function(json){
		        isloading=false;	
		        if(json.retcode=='0000'){
		        	at_loading.hide();
		        	at_infoTrace.show(json.retmsg,"i");
		        	me.box[0].reset();
		        	clearForm();
		            //if(json.url){
		            //    location.href="safetyInfo_addPage.do";
		            //}
		        }else{
		            at_loading.hide();
		            at_infoTrace.show(json.retmsg,"e");
		        }
		    }
		});
	}
	
	//重置表单
	var clearForm = function(){
		inPass(me.seqId);
	};
	
	//this.seqId.focus(function(){
	//	if(strTrimLength(me.seqId)==0)
	//		txt(me.seqId).append(seqId_o);
	//	me.sCheck=false;
	//})
	//延时200毫秒才触发验证
	//this.seqId.blur(function(){setTimeout(function(){ me.seqIdCheck(me.seqId)},200)})	
	
	this.deptName.focus(function(){
		if(strTrimLength(me.deptName)==0)
		  txt(me.deptName).append(deptName_e1);
		me.deptName.isCheck=false;
	})
	this.deptName.blur(function(){me.deptNameCheck(me.deptName)});
	this.deptName.keyup(function(){
	    var length=strTrimLength(me.deptName);
	    if(length==0){
	        me.deptName.focus();
	    }
	});
	
	//第一个控件获得焦点
	//this.input.eq(0).focus();	
	
	//me.box.submit(function(){
	//    me.btn.click();
	//    return true;
	//});
}
